package com.system.utils;

import com.aspose.cad.Color;
import com.aspose.cad.Image;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PdfOptions;
import com.aspose.cad.internal.B.F;
import com.system.controller.ReservePlandrawingController;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.chainsaw.Main;
import org.joda.time.DateTime;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.util.Date;

public class DWGFileToPDFManager {

    /**

     *

     * @param file2 选择dwg文件路径

     * @param //dataDir 保存FTP文件路径

     */

    public static boolean DWGFileToPDF(File file2) throws IOException {
        System.out.println("写文件中");
        String dateTime = new DateTime().toString("yyyy-MM-dd HH-mm-ss-sss");
        long b= System.currentTimeMillis();
        System.out.println(dateTime);
        String end_format =file2.getName().substring(file2.getName().lastIndexOf(".")+1);//截取最后一个"/"后面所有的字符串
        System.out.println(end_format);
String pathName=ReservePlandrawingController.FILE_PATH +file2.getName();
System.out.println(pathName);
        if(end_format.equals("dwg")) {


        File file=new File(pathName.substring(0,pathName.lastIndexOf("."))+".pdf");
        if(file.exists()){return true;}

            Image objImage = Image.load(String.valueOf(file2));
        CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
        rasterizationOptions.setBackgroundColor(Color.getWhite());
        rasterizationOptions.setPageWidth(1600);

        rasterizationOptions.setPageHeight(1600);

// Create an instance of PdfOptions

        PdfOptions pdfOptions = new PdfOptions();

// Set the VectorRasterizationOptions property

        pdfOptions.setVectorRasterizationOptions(rasterizationOptions);

// Export the DWG to PDF
        objImage.save(pathName.substring(0,pathName.lastIndexOf("."))+".pdf", pdfOptions);
       if(!file.exists()){return false; }
            String endTime = new DateTime().toString("yyyy-MM-dd HH-mm-ss-sss");
            long c= System.currentTimeMillis();
            System.out.println(endTime);
            System.out.println(c-b);
       return true;
        }
        return false;
    }








    public static void DWGFileToPDF2(File dwgFile) throws IOException{

System.out.println("DWGFileToPDF2");

        FileInputStream fileInputStream;
        //将pdf文件转换成InputStream输入流
        fileInputStream = new FileInputStream(dwgFile);
        System.out.println("DWGFileToPDF000000");
        Image objImage = Image.load(fileInputStream);
        CadRasterizationOptions rasterizationOptions = new  CadRasterizationOptions();
        System.out.println("DWGFileToPDF211111");
        //设置属性
        rasterizationOptions.setBackgroundColor(Color.getBlack());
        rasterizationOptions.setPageWidth(1400);
        rasterizationOptions.setPageHeight(650);
        rasterizationOptions.setAutomaticLayoutsScaling(true);
        rasterizationOptions.setNoScaling (false);
        rasterizationOptions.setDrawType(1);

        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setVectorRasterizationOptions(rasterizationOptions);

        System.out.println("DWGFileToPDF222222");
        //输出文件
        File outputFile = new File(dwgFile.getName().substring(0,dwgFile.getName().lastIndexOf("."))+".pdf");
        //存放地址
        System.out.println("DWGFileToPDF23333");
        //outputFile.getPath() 地址
        objImage.save(outputFile.getPath(), pdfOptions);

        System.out.println(dwgFile.getName()+"转换成功！");
        fileInputStream.close();
    }






    /**
     * 方法描述  CAD文件转换为PDF流
     * @param inputStream 选择CAD文件流
     * @author Tarzan Liu
     * @date 2020年01月08日 15:08:50
     */
    public static  void CADFileToPDF(InputStream inputStream) throws Exception {
//        RUNNING = true;
        Image image = Image.load(inputStream);
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        cadRasterizationOptions.setBackgroundColor(Color.getWhite());
        cadRasterizationOptions.setPageWidth(1600);
        cadRasterizationOptions.setPageHeight(1600);
        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        image.save(out,pdfOptions);
//        return  outputStreamConvertInputStream(out);
    }



    public static void main(String[] args) throws IOException {
        String dateTime = new DateTime().toString("yyyy-MM-dd HH-mm-ss-sss");
        long b= System.currentTimeMillis();
        System.out.println(dateTime);
String a="C:\\Users\\fwy\\Desktop\\CCD线型表.dwg";
File file=new File(a);
//        CommonsMultipartFile cFile = (CommonsMultipartFile) file2;
//        DiskFileItem fileItem = (DiskFileItem) cFile.getFileItem();
//        InputStream inputStream1 = fileItem.getInputStream();

      //  MultipartFile file2 =new CommonsMultipartFile(item);
//1      DWGFileToPDF2(file);
        DWGFileToPDF(file);
        String endTime = new DateTime().toString("yyyy-MM-dd HH-mm-ss-sss");
        long c= System.currentTimeMillis();
        System.out.println(endTime);
        System.out.println(c-b);
        //FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);

    }





    }


